//
// Created by 12967 on 2022/1/14.
//

#include <string>
#include <vector>
#include <io.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include "FileFinder.h"

using namespace std;


void getFiles(const char *path, const string& exd, vector<string>&files)
{
    long hFile=0;
    //path=R"(E:\DataStructure\category)";
    struct _finddata_t fileinfo;
    string pathName,exdName;

    if (0 != strcmp(exd.c_str(), "")) exdName="\\*."+exd;
    else exdName="\\*";

    if((hFile=_findfirst(pathName.assign(path).append(exdName).c_str(),&fileinfo))!=-1)
    {
        do
        {
            if(strcmp(fileinfo.name,".")!=0&&strcmp(fileinfo.name,"..")!=0)
                files.emplace_back(fileinfo.name);
        }while(_findnext(hFile, &fileinfo)==0);
        _findclose(hFile);
    }
}

void printFiles(const  vector<string>& files){
    for(const auto & file : files){
        cout<<file<<" ";
    }
    cout<<endl;
}

void readFiles(const string& currentFile,HashList* list){
    ifstream fileReader;
    string path=R"(E:\DataStructure\category\)";
    path+=currentFile;
    fileReader.open(path);
    string s;
    while(fileReader>>s){
        if((s[0]>64&&s[0]<91)||(s[0]>96&&s[0]<123)){
            char key=s[s.size()-1];
            if(!((key>64&&key<91)||(key>96&&key<123))){
                s=s.substr(0,s.size()-1);
            }
            transform(s.begin(),s.end(),s.begin(),::tolower);
            list->insert(s,currentFile);
        }
    }
    cout<<currentFile+" has been successfully saved!"<<endl;
    fileReader.close();
}

void readFiles(const string& currentFile,AVLtree* tree){
    ifstream fileReader;
    string path=R"(E:\DataStructure\category\)";
    path+=currentFile;
    fileReader.open(path);
    string s;
    while(fileReader>>s){
        if((s[0]>64&&s[0]<91)||(s[0]>96&&s[0]<123)){
            char key=s[s.size()-1];
            if(!((key>64&&key<91)||(key>96&&key<123))){
                s=s.substr(0,s.size()-1);
            }
            transform(s.begin(),s.end(),s.begin(),::tolower);
            tree->insert(s,currentFile);
        }
    }
    cout<<currentFile+" has been successfully saved!"<<endl;
    fileReader.close();
}

void readFiles(const string& currentFile,trieTree* ttree){
    ifstream fileReader;
    string path=R"(E:\DataStructure\category\)";
    path+=currentFile;
    fileReader.open(path);
    string s;
    while(fileReader>>s){
        if((s[0]>64&&s[0]<91)||(s[0]>96&&s[0]<123)){
            bool isright=true;
            for(char i : s){
                if(!((i>64&&i<91)||(i>96&&i<123))){
                    isright=false;
                    break;
                }
            }
            char key=s[s.size()-1];
            if(!((key>64&&key<91)||(key>96&&key<123))){
                s=s.substr(0,s.size()-1);
            }
            transform(s.begin(),s.end(),s.begin(),::tolower);
            if(isright)
            ttree->insert(s,currentFile);
        }
    }
    cout<<currentFile+" has been successfully saved!"<<endl;
    fileReader.close();
}